#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/8/6 下午2:18
# @Author  : Samge
import os


def replace_import_path():
    """
    修复rpc转换后的文件 import的路径不完整 的问题
    :return:
    """
    rpc_path = './rpc_package'
    for py_file in os.listdir(rpc_path):
        if not py_file.endswith('.py'):
            continue
        print(f"正在处理：{py_file}")
        py_file = f"{rpc_path}/{py_file}"
        with open(py_file, "r", encoding='UTF-8') as f:
            temp_txt = f.read() or ''
            temp_txt = temp_txt.replace('import helloWord_pb2', 'import rpc_package.helloWord_pb2')
            temp_txt = temp_txt.replace('import helloWord_pb2_grpc', 'import rpc_package.helloWord_pb2_grpc')
            f.close()
        with open(py_file, "w", encoding='UTF-8') as f:
            f.write(temp_txt)
            f.close()
    print("任务操作完成")


if __name__ == '__main__':
    replace_import_path()
